package com.ruoyi.purchase.service.impl;

import java.util.List;

import com.ruoyi.base.domain.Customer;
import com.ruoyi.base.domain.GasBalance;
import com.ruoyi.base.mapper.CustomerMapper;
import com.ruoyi.base.mapper.GasBalanceMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.purchase.mapper.PaymentRecordMapper;
import com.ruoyi.purchase.domain.PaymentRecord;
import com.ruoyi.purchase.service.IPaymentRecordService;

/**
 * 付款记录
Service业务层处理
 *
 * @author h w
 * @date 2022-12-09
 */
@Service
public class PaymentRecordServiceImpl implements IPaymentRecordService
{
    @Autowired
    private PaymentRecordMapper paymentRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private GasBalanceMapper gasBalanceMapper;

    /**
     * 查询付款记录

     *
     * @param paymentRecordId 付款记录
主键
     * @return 付款记录

     */
    @Override
    public PaymentRecord selectPaymentRecordByPaymentRecordId(Long paymentRecordId)
    {
        return paymentRecordMapper.selectPaymentRecordByPaymentRecordId(paymentRecordId);
    }

    /**
     * 查询付款记录
列表
     *
     * @param paymentRecord 付款记录

     * @return 付款记录

     */
    @Override
    public List<PaymentRecord> selectPaymentRecordList(PaymentRecord paymentRecord)
    {
        return paymentRecordMapper.selectPaymentRecordList(paymentRecord);
    }

    /**
     * 新增付款记录

     *
     * @param paymentRecord 付款记录

     * @return 结果
     */
    @Override
    public int insertPaymentRecord(PaymentRecord paymentRecord)
    {
        paymentRecord.setCreateTime(DateUtils.getNowDate());
        Customer paymentUnit = customerMapper.selectCustomerByCustomerId(Long.valueOf(paymentRecord.getPaymentUnit()));
        Customer receiveUnit = customerMapper.selectCustomerByCustomerId(Long.valueOf(paymentRecord.getReceiveUnit()));
        if(receiveUnit != null && paymentUnit != null) {
            GasBalance balance = new GasBalance();
            balance.setCustomerId(paymentUnit.getCustomerId());
            balance.setEnterpriseId(receiveUnit.getCustomerId());
            List<GasBalance> balanceList = gasBalanceMapper.selectGasBalanceList(balance);
            if(balanceList == null || balanceList.size() <= 0) {
                balance.setCustomerName(paymentUnit.getCustomerName());
                balance.setEnterpriseName(receiveUnit.getCustomerName());
                balance.setCustomerBalance(paymentRecord.getPaymentAmount());
                balance.setCreateBy(paymentRecord.getCreateBy());
                balance.setCreateTime(DateUtils.getNowDate());
                gasBalanceMapper.insertGasBalance(balance);
            } else {
                GasBalance balanceModify = balanceList.get(0);
                balanceModify.setCustomerBalance(balanceModify.getCustomerBalance().add(paymentRecord.getPaymentAmount()));
                balanceModify.setUpdateBy(paymentRecord.getCreateBy());
                balanceModify.setUpdateTime(DateUtils.getNowDate());
                gasBalanceMapper.updateGasBalance(balanceModify);
            }
            return paymentRecordMapper.insertPaymentRecord(paymentRecord);
        }
        return 0;
    }

    /**
     * 修改付款记录

     *
     * @param paymentRecord 付款记录

     * @return 结果
     */
    @Override
    public int updatePaymentRecord(PaymentRecord paymentRecord)
    {
        paymentRecord.setUpdateTime(DateUtils.getNowDate());
        return paymentRecordMapper.updatePaymentRecord(paymentRecord);
    }

    /**
     * 批量删除付款记录

     *
     * @param paymentRecordIds 需要删除的付款记录
主键
     * @return 结果
     */
    @Override
    public int deletePaymentRecordByPaymentRecordIds(Long[] paymentRecordIds)
    {
        return paymentRecordMapper.deletePaymentRecordByPaymentRecordIds(paymentRecordIds);
    }

    /**
     * 删除付款记录
信息
     *
     * @param paymentRecordId 付款记录
主键
     * @return 结果
     */
    @Override
    public int deletePaymentRecordByPaymentRecordId(Long paymentRecordId)
    {
        return paymentRecordMapper.deletePaymentRecordByPaymentRecordId(paymentRecordId);
    }
}
